package com.kcl.dfss;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.kcl.dfss.http.ServerConnection;
import com.kcl.dfss.utils.AppUtils;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class FAQActivity extends Activity {

	protected static final String tag = "FAQActivity";
	private LinearLayout faq_back;
	private ListView faq_list;
	private ProgressBar progress;
	private List<Map<String, String>> list = null;
	private Handler handler;
	private boolean isNetEnabled = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_faq);
		
		faq_back = (LinearLayout) findViewById(R.id.faq_back);
		faq_list = (ListView) findViewById(R.id.faq_list);
		
		progress = (ProgressBar) findViewById(R.id.diao);
		
		isNetEnabled = AppUtils.isNetworkAvailable(FAQActivity.this);
		if(isNetEnabled){
			progress.setVisibility(View.VISIBLE);
			getlist();
		}else{
			progress.setVisibility(View.GONE);
			Toast.makeText(FAQActivity.this, "当前无网络连接", Toast.LENGTH_SHORT).show();
		}
		
		faq_back.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		handler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				if(msg.what == 1){
					SimpleAdapter adapter = new SimpleAdapter(FAQActivity.this, list, R.layout.faq_item, 
							new String[]{"title","content"}, new int[]{R.id.t_title,R.id.t_content});
					faq_list.setAdapter(adapter);
					progress.setVisibility(View.GONE);
				}
			}
		};
		
		
		
	}
	
	public void getlist(){
		new Thread(new Runnable() {
			@Override
			public void run() {
				String faq = ServerConnection.GetFaq("");
				Log.i(tag, "返回数据："+faq);
				if(faq.equals("exception")){;
					runOnUiThread(new Runnable() {
						public void run() {
							Toast.makeText(FAQActivity.this, "网络异常，请检查网络连接", Toast.LENGTH_SHORT).show();
						}
					});
				}else{
					try {
						JSONObject object = new JSONObject(faq);
						JSONArray jsonArray = object.getJSONArray("data");
						list = new ArrayList<Map<String,String>>();
						for (int i = 0; i < jsonArray.length(); i++) {
							JSONObject items = (JSONObject) jsonArray.get(i);
							Map<String, String> listItem = new HashMap<String, String>();
							listItem.put("title", items.getString("title"));
							listItem.put("content", items.getString("content"));
							list.add(listItem);
						}
						Message message = new Message();
						message.what = 1;
						handler.sendMessage(message);
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}

}
